﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using FarmacieDAL;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;

namespace Farmacie.Controls
{
    /// <summary>
    /// Interaction logic for Produse.xaml
    /// </summary>
    public partial class Medicamente : UserControl, INotifyPropertyChanged
    {
        private List<Medicament> toateMedicamentele = new List<Medicament>();
        private CollectionViewSource medicamente;
        private Medicament medicamentToInsert = new Medicament();

        public Medicament MedicamentToInsert
        {
            get
            {
                return medicamentToInsert;
            }
            set
            {
                if (value != medicamentToInsert)
                {
                    medicamentToInsert = value;
                    OnPropertyChanged("MedicamentToInsert");
                }
            }
        }

        public ICollectionView ToateMedicamentele
        {
            get
            {
                return medicamente.View;
            }
        }

        public IPagedCollectionView MedicamenteAfisate
        {
            get { return radPager.PagedSource; }
        }


        public Medicamente()
        {
            InitializeComponent();

            toateMedicamentele.Clear();
            toateMedicamentele.AddRange(MedicamenteManager.ToateMedicamentele);

            medicamente = new CollectionViewSource();
            medicamente.Source = toateMedicamentele;

            this.DataContext = this;
            gridMedicamente.RowEditEnded += gridMedicamente_RowEditEnded;
            medicamentInsertControl.InitializeControl(RefreshGrid);
            this.Loaded += new RoutedEventHandler(Medicamente_Loaded);
        }

        void Medicamente_Loaded(object sender, RoutedEventArgs e)
        {
            OnPropertyChanged("MedicamenteAfisate");
        }

        public void RefreshGrid()
        {
            MedicamenteManager.Refresh();
            StocCriticManager.Refresh();

            toateMedicamentele.Clear();
            toateMedicamentele.AddRange(MedicamenteManager.ToateMedicamentele);
            ToateMedicamentele.Refresh();
            radPager.PageIndex = 1;
            OnPropertyChanged("MedicamenteAfisate");
        }

        private void buttonDelete_Sterge(object sender, RoutedEventArgs e)
        {
            RadButton deleteButton = sender as RadButton;

            if (deleteButton != null)
            {
                Medicament medicamentToDelete = deleteButton.DataContext as Medicament;

                medicamentToDelete.Deleted = true;

                FarmacieDALManager.EntitiesContext.SaveChanges();
                RefreshGrid();
            }
        }

        private void gridMedicamente_RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e)
        {
            Medicament changedMedicament = e.Row.DataContext as Medicament;

            if (changedMedicament != null)
            {
                FarmacieDALManager.EntitiesContext.SaveChanges();
                RefreshGrid();
            }
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
